<?php

/**
 * WeEngine System
 *
 * (c) We7Team 2021 <https://www.w7.cc>
 *
 * This is not a free software
 * Using it under the license terms
 * visited https://www.w7.cc for more details
 */

namespace app\controller;

use app\BaseController;
use app\model\News;

class IndexController extends BaseController
{
    public function index()
    {
        $data = ['label' => $this->request->get('label')];
        $this->validate($data, 'app\validate\Index.index');
        
        $newsModel = new News();
        $menu      = $newsModel->getAllLabel();
        if ($menu) {
            $menu = array_column($menu->toArray(), 'label');
        }
        $list = $newsModel->lists($data['label'] ?? '');
        return view('index/index', ['list' => $list, 'menu' => array_unique($menu)]);
    }

    public function news()
    {
        $data = ['id' => $this->request->get('id')];
        $this->validate($data, 'app\validate\Index.news');
        
        $newsInfo = News::find($data['id']);
        if (empty($newsInfo)) {
            throw new \RuntimeException('文章不存在！');
        }
        return view('index/detail', ['info' => $newsInfo]);
    }
}
